<?php
require_once '../sub/init.php';
header("Cache-control: private");
define('ZEAI2',dirname(__FILE__).DIRECTORY_SEPARATOR);
require_once 'chkUadm.php';
require_once ZEAI.'cache/udata.php';

if(!in_array('crm_pay_analyse',$QXARR) && !CRM_ADM)exit(noauth());
$SQL = "";

//非超管匹配自己门店
if(!CRM_ADM)$SQL.=" AND agentid=$session_agentid";
//超管搜索按门店
if (ifint($agentid) && CRM_ADM){
	$row = $db->ROW(__TBL_CRM_AGENT__,"title","id=".$agentid);
	if ($row)$agenttitle= dataIO($row[0],'out');
	$SQL .= " AND agentid=$agentid";
}else{
	$agenttitle=$session_agenttitle;
}

//根据日期总
if(ifdate($date1)){
	$sDATE1_ = strtotime($date1.' 00:00:01');
	$SQL  .= " AND addtime >= $sDATE1_";
}
if(ifdate($date2)){
	$sDATE2_ = strtotime($date2.' 23:59:59');
	$SQL  .= " AND addtime <= $sDATE2_";
}

//录入红娘
$Skey2 = trimhtml($Skey2);
if(ifint($Skey2)){
	$SQL .= " AND (admid=$Skey2) ";
}elseif(!empty($Skey2)){
	$SQL .= " AND (admname LIKE '%".$Skey2."%' )";
}


if($submitok=='ajax_reg_day'){
	if(ifdate($date1)){
		$date1 = strtotime($date1);
		$SQL  .= " AND addtime >= $date1";
	}
	if(ifdate($date2)){
		$date2 = strtotime($date2);
		$SQL  .= " AND addtime <= $date2";
	}
	if(empty($SQL)){
		$LIMIT = " LIMIT 30";
	}else{
		$LIMIT = "";
	}
	$rt=$db->query("SELECT SUM(price) AS num,from_unixtime(addtime,'%Y-%m-%d') AS day FROM ".__TBL_CRM_HT__." WHERE payflag=1".$SQL." GROUP BY day ORDER BY day DESC".$LIMIT);
	$total = $db->num_rows($rt);
	if ($total <= 0) {
		exit('');
	} else {
		while($tmprows = $db->fetch_array($rt,'name')){$arr[]=$tmprows;}
		$arrnew = array_reverse($arr);
		foreach ($arrnew as $rows) {
			$day = $rows['day'];
			$num = $rows['num'];
			$arr[]=array("'".$day."'".",".$num);
		}
		exit(encode_json($arr));
	}
}

$rt=$db->query("SELECT SUM(price) AS num FROM ".__TBL_CRM_HT__." WHERE payflag=1".$SQL);
$total = $db->num_rows($rt);
$totalnum = 0;
if ($total > 0) {
	$row = $db->fetch_array($rt,'num');
	$totalnum  = $row[0];
}
if (!empty($agenttitle))$agenttitle='【'.$agenttitle.'】';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script src="js/highcharts.js"></script> 
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<link href="css/crm.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
.box{padding-top:10px;padding-bottom:20px;min-width:1230px}
</style>
</head>
<body>
<form name="form1" method="get" action="<?php echo SELF; ?>">
<div class="box">
	<div class="topsearch" style="line-height:50px;font-size:18px;text-align:center;font-weight:bold"><?php echo $agenttitle;?>财务统计</div>
    <div class="topsearch">
		<dl>
            <dt style="margin-top:1px">筛选筛选</dt>
            <dd>
            <!--超管按门店查询-->
            <?php if(CRM_ADM){
                $rt2=$db->query("SELECT id,title FROM ".__TBL_CRM_AGENT__." WHERE flag=1 ORDER BY px DESC,id DESC");
                $total2 = $db->num_rows($rt2);
                if ($total2 > 0) {?>
                    <select name="agentid" class="W200 size2 picmiddle" style="margin-right:10px">
                    <option value="">全部门店</option>
                    <?php
                        for($j=0;$j<$total2;$j++) {
                            $rows2 = $db->fetch_array($rt2,'num');
                            if(!$rows2) break;
                            $clss=($agentid==$rows2[0])?' selected':'';?>
                            <option value="<?php echo $rows2[0];?>"<?php echo $clss;?>><?php echo dataIO($rows2[1],'out');?></option>
                            <?php
                        }
                    ?>
                    </select>
                    <?php
                }
                ?>
            <?php }?>
            <!---->
            <input name="Skey2" type="text" maxlength="25" class="input size2 W150" placeholder="录入红娘姓名/ID" value="<?php echo $Skey2; ?>">
            <input name="date1" type="text" id="date1" maxlength="25" class="input W100 size2" placeholder="起始时间" value="<?php echo $date1; ?>"  autocomplete="off">　～　
            <input name="date2" type="text" id="date2" maxlength="25" class="input W100 size2" placeholder="结束时间" value="<?php echo $date2; ?>"  autocomplete="off">　
            <input type="submit" value="开始统计" class="btn size2 QING" />
            </dd>
        </dl>
    </div>
    <?php if ($totalnum>0){?>
        <div id="container2" style="width:90%; height:520px;margin:20px auto"></div>
    <?php }else{
    	echo "<div class='nodataico'><i></i>暂无财务信息<br><a class='aQINGed' href='javascript:history.back(-1)'>返回</a></div>";
    }?>
    <div class="clear"></div>
</div>
</form>
<script>
	<?php if ($totalnum>0){?>
	setTimeout(function(){
		zeai.ajax({url:'crm_pay_analyse'+zeai.ajxext+'submitok=ajax_reg_day&agentid=<?php echo $agentid;?>&date1=<?php echo $date1;?>&date2=<?php echo $date2;?>'},function(e){
			if(!zeai.empty(e)){
				var arr=e;
				arr = arr.replace(/"/g,"");
				arr = zeai.jsoneval(arr);
				reg(arr);
			}
		});
	},200);
	<?php }?>
	
	function reg(arr){
		Highcharts.chart('container2', {
			chart: {type: 'column'},
			title: {text: '<?php echo $agenttitle;?>收益统计（总计：￥<?php echo $totalnum;?>）'},
			subtitle: {text: ''},
			credits: {"enabled": false},
			xAxis: {
				type: 'category',
				labels: {
					rotation: -45,
					style: {fontSize: '12px',fontFamily: 'Verdana, sans-serif'}
				}
			},
			yAxis: {min: 0,title: {text:'合同已审金额（元）'}},
			legend: {enabled: false},
			tooltip: {pointFormat: '<b style="font-weight:normal;color:#666">￥{point.y:.0f}</b>'},
			colors: ["#7CC7C0"],
			series: [{
				name: 'zeai.cn',
				data: arr,
				dataLabels: {
					enabled: true,
					rotation:0,
					color: '#ffffff',
					align: 'center',
					format: '￥{point.y:.0f}', // one decimal
					y:22, //10 pixels down from the top
					style:{fontSize:'12px',fontFamily:'Arial,sans-serif'}
				}
			}]
		});
	}
</script>
<script src="laydate/laydate.js"></script>
<script>
    laydate.render({
        theme: 'molv'
        ,elem: '#date1'
        ,type: 'date'
    }); 
    laydate.render({
        theme: 'molv'
        ,elem: '#date2'
        ,type: 'date'
    }); 
</script>
<?php require_once 'bottomadm.php';?>